package AI;

import java.util.Arrays;
import utils.POINT;

public class ChessAlgorithm {

    // addition from chess define
    public final int MW = 32, SW = 1;
    public final int BWA = MW + SW * 2;

    public final int XBW = BWA * 9, YBW = BWA * 10;
    //XBW = 594, YBW = 660

    public final int MAN = 0;
    public final int COM = 1;

    public final int RED = 0;
    public final int BLACK = 1;

    public final int RED_K = 0;
    public final int RED_S = 1;
    public final int RED_X = 2;
    public final int RED_M = 3;
    public final int RED_J = 4;
    public final int RED_P = 5;
    public final int RED_B = 6;

    public final int BLACK_K = 7;
    public final int BLACK_S = 8;
    public final int BLACK_X = 9;
    public final int BLACK_M = 10;
    public final int BLACK_J = 11;
    public final int BLACK_P = 12;
    public final int BLACK_B = 13;

    // 33 elements
    public final int[] ManToIcon = {0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 13, 13, 13, -1};

    // 33 elements
    public final int[] ManToType = {0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, -1};

    //33 elements
    public static final int[] SideOfMan = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1};

    // 11x12
    public final int[][] _defaultmap
            = {
                //[0][1][2][3][4][5][6][7][8][9][10][11]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[0]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[1]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[2]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[3]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[4]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[5]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[6]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[7]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[8]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},//[9]
                {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32}//[10]
            };

    // 2
    public static final int[] FistOfSide = {0, 16};
    //2
    public final int[] LastOfSide = {15, 31};
    public final int MAXMOVE = 1000;
    // end addition

    final int[] base = {300, 400, 300, 600, 1000, 600, 300};	//average value
    final int[] range = {0, 0, 0, 20, 10, 0, 50};	    //range of average change
    //exmaple: Ma average value=200, change range =13%, so base[3]=200 and range[3]=13
    final int[] BV1 =//basic value
            {
                base[0] - base[0] * range[0] / 100,
                base[1] - base[1] * range[1] / 100,
                base[3] - base[2] * range[2] / 100,
                base[3] - base[3] * range[3] / 100,
                base[4] - base[4] * range[4] / 100,
                base[5] - base[5] * range[5] / 100,
                base[6] - base[6] * range[6] / 100
            };
    final int[] BV2 =//active value
            {
                2 * base[0] * range[0] / 100 / 4,
                2 * base[1] * range[1] / 100 / 4,
                2 * base[2] * range[2] / 100 / 4,
                2 * base[3] * range[3] / 100 / 8,
                2 * base[4] * range[4] / 100 / 17,
                2 * base[5] * range[5] / 100 / 17,
                0,};

    final int[] BV3 =//added value for bing in different places
            {
                0 * 2 * base[6] * range[6] / 100 / 4,
                1 * 2 * base[6] * range[6] / 100 / 4,
                2 * 2 * base[6] * range[6] / 100 / 4,
                3 * 2 * base[6] * range[6] / 100 / 4,
                4 * 2 * base[6] * range[6] / 100 / 4,};

    //value of bing in different places
    final int[][][] ManBPlus
            = {
                {
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 1, 2, 3, 4, 4, 4, 3, 2, 1, 0},
                    {0, 1, 2, 3, 4, 4, 4, 3, 2, 1, 0},
                    {0, 1, 2, 3, 3, 3, 3, 3, 2, 1, 0},
                    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
                    {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
                    {0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
                },
                {
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0},
                    {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
                    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
                    {0, 1, 2, 3, 3, 3, 3, 3, 2, 1, 0},
                    {0, 1, 2, 3, 4, 4, 4, 3, 2, 1, 0},
                    {0, 1, 2, 3, 4, 4, 4, 3, 2, 1, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
                }
            };
    // static members
    int kV;
    boolean flagV;
    int iE, jE, nE, xE, yE;
    boolean flagE;
    int kCV;
    int iCV, jCV, nCV, xCV, yCV;
    boolean flagCV;

    // normal members
    public int S_WIDTH;
    public int S_DEPTH;
    public int contactpercent1;	//emphasis on defend
    public int contactpercent2;	//emphasis on attack

    public ChessAlgorithm() {
        S_WIDTH = 8;
        S_DEPTH = 6;
        contactpercent1 = 24;	//emphasis on defend
        contactpercent2 = 26;	//emphasis on defend
    }

    boolean NORED(int[][] tmap, int i, int j) {
        return SideOfMan[tmap[i][j]] != 0;
    }

    boolean NOBLACK(int[][] tmap, int i, int j) {
        return SideOfMan[tmap[i][j]] != 1;
    }

    boolean NOMAN(int[][] tmap, int i, int j) {
        return tmap[i][j] == 32;
    }

    public void quickSort(int[] A, int[] chessman, POINT[] targetpoint, int low, int high) {
        int pivot;
        int pivot_man;
        POINT pivot_point = new POINT();
        int scanUp, scanDown;
        int mid, k;
        POINT point = new POINT();
        if (high - low <= 0) {
            return;
        } else {
            if (high - low == 1) {
                if (A[high] > A[low]) {
                    k = A[high];
                    A[high] = A[low];
                    A[low] = k;
                    k = chessman[high];
                    chessman[high] = chessman[low];
                    chessman[low] = k;
                    point.x = targetpoint[high].x;
                    point.y = targetpoint[high].y;
                    targetpoint[high].x = targetpoint[low].x;
                    targetpoint[high].y = targetpoint[low].y;
                    targetpoint[low].x = point.x;
                    targetpoint[low].y = point.y;
                    return;
                }
            }
        }
        mid = (low + high) / 2;
        pivot = A[mid];
        pivot_man = chessman[mid];
        pivot_point.x = targetpoint[mid].x;
        pivot_point.y = targetpoint[mid].y;
        k = A[mid];
        A[mid] = A[low];
        A[low] = k;
        k = chessman[mid];
        chessman[mid] = chessman[low];
        chessman[low] = k;
        point.x = targetpoint[mid].x;
        point.y = targetpoint[mid].y;
        targetpoint[mid].x = targetpoint[low].x;
        targetpoint[mid].y = targetpoint[low].y;
        targetpoint[low].x = point.x;
        targetpoint[low].y = point.y;
        scanUp = low + 1;
        scanDown = high;
        do {
            while (scanUp <= scanDown && A[scanUp] >= pivot) {
                scanUp++;
            }
            while (pivot > A[scanDown]) {
                scanDown--;
            }
            if (scanUp < scanDown) {
                k = A[scanUp];
                A[scanUp] = A[scanDown];
                A[scanDown] = k;
                k = chessman[scanUp];
                chessman[scanUp] = chessman[scanDown];
                chessman[scanDown] = k;
                point.x = targetpoint[scanUp].x;
                point.y = targetpoint[scanUp].y;
                targetpoint[scanUp].x = targetpoint[scanDown].x;
                targetpoint[scanUp].y = targetpoint[scanDown].y;
                targetpoint[scanDown].x = point.x;
                targetpoint[scanDown].y = point.y;
            }
        } while (scanUp < scanDown);
        A[low] = A[scanDown];
        A[scanDown] = pivot;
        chessman[low] = chessman[scanDown];
        chessman[scanDown] = pivot_man;
        targetpoint[low].x = targetpoint[scanDown].x;
        targetpoint[low].y = targetpoint[scanDown].y;
        targetpoint[scanDown].x = pivot_point.x;
        targetpoint[scanDown].y = pivot_point.y;

        if (low < scanDown - 1) {
            quickSort(A, chessman, targetpoint, low, scanDown - 1);
        }
        if (scanDown + 1 < high) {
            quickSort(A, chessman, targetpoint, scanDown + 1, high);
        }
    }

    public int Value(int[][] tmap, POINT[] tmanposition, IntegerWrapper tside) {
        int[] ManExtValue = new int[32];
        int[] ManBaseValue = new int[32];
        int[][] ManContact = new int[32][32];
        int[] BeAteCount = new int[32];
        boolean[] OwnSee = new boolean[32];

        Arrays.fill(ManExtValue, 0);
        //Arrays.fill(ManContact, 0);
        for (int i = 0; i < 32; i++) {
            for (int j = 0; j < 32; j++) {
                ManContact[i][j] = 0;
            }
        }
        Arrays.fill(ManBaseValue, 0);
        Arrays.fill(BeAteCount, 0);
        Arrays.fill(OwnSee, false); // zeromemory with bool array is the same with this statement?

        int maxvalue = 0;
        int i, j;
        ContactV(tmap, tmanposition, tside, ManBaseValue, ManContact);
        //MyWriter fw2 = new MyWriter("D:\\nb.txt");
        //fw2.Write3232Matrix(ManContact, "Mancontact");
        //fw2.Write32Matrix(ManBaseValue, 32, "manbase");
        for (i = FistOfSide[tside.getNumb()]; i <= LastOfSide[tside.getNumb()]; i++) {
            int fl;
            fl = (tside.getNumb() == 0) ? 1 : 0;
            if (ManContact[i][FistOfSide[fl]] != 0) {
                maxvalue = 9700;
                return maxvalue;
            }
        }
        for (i = 0; i < 32; i++) {
            kV = ManToType[i];
            ManBaseValue[i] = BV1[kV] + ManBaseValue[i] * BV2[kV];
            switch (kV) {
                case 6:
                    ManBaseValue[i] += BV3[ ManBPlus[SideOfMan[i]][tmanposition[i].y][tmanposition[i].x]];
                    break;
            }
        }

        for (i = 0; i < 32; i++) {
            for (j = 0; j < 32; j++) {
                if (ManContact[i][j] == 1) {
                    if (SideOfMan[i] == SideOfMan[j]) {
                        BeAteCount[j]++;
                        if (!OwnSee[j]) {
                            ManExtValue[i] += ManBaseValue[j] * contactpercent1 / 100;//ours
                            OwnSee[j] = true;
                        }
                    } else {
                        ManExtValue[i] += ManBaseValue[j] * contactpercent2 / 100;//opponents
                        BeAteCount[j]--;
                    }
                }
            }
        }
        for (i = FistOfSide[tside.getNumb()]; i <= LastOfSide[tside.getNumb()]; i++) {
            if (tmanposition[i].x != 0) {
                maxvalue += ManBaseValue[i] + ManExtValue[i];
            }
        }
        flagV = false;
        kV = 32;
        int index = 1;
        if (tside.getNumb() != 0) {
            index = 0;
        }
        for (i = FistOfSide[index]; i <= LastOfSide[index]; i++) {
            if (tmanposition[i].x != 0) {
                maxvalue -= ManBaseValue[i] + ManExtValue[i];
            }
            if (ManContact[i][FistOfSide[tside.getNumb()]] != 0) {
                flagV = true;
                kV = i;
                break;
            }
        }
        if (flagV && BeAteCount[kV] >= 0)//get checked, check man cann't be eaten
        {
            j = 0;
            for (i = FistOfSide[tside.getNumb()]; i <= LastOfSide[tside.getNumb()]; i++) {
                if (BeAteCount[i] < 0 && ManBaseValue[i] > j) {
                    j = ManBaseValue[i];
                }
            }
            maxvalue -= j;
        } else {
            j = 0;
            int index2 = 1;
            if (tside.getNumb() != 0) {
                index2 = 0;
            }

            for (i = FistOfSide[index2]; i <= LastOfSide[index2]; i++) {
                if (BeAteCount[i] < 0 && ManBaseValue[i] > j) {
                    j = ManBaseValue[i];
                }
            }
            maxvalue += j;
        }
        return maxvalue;
    }

    public boolean Think(int[][] tmap, POINT[] tmanposition, IntegerWrapper tside, IntegerWrapper resultman, POINT resultpoint) {
        int[] chessman = new int[125];
        POINT[] targetpoint = new POINT[125];

        //diffirent
        for (int i = 0; i < 125; ++i) {
            targetpoint[i] = new POINT();
        }
        // diffirent
        IntegerWrapper count = new IntegerWrapper(0);

        int maxvalue;
        int cur;
        int curvalue;

        if (EnumList(tmap, tmanposition, tside, chessman, targetpoint, count)) {
            // WriteEnumList(tmap,tmanposition,tside,chessman,targetpoint,count);
            //for(int i=0; i<45; ++i)
            //   System.out.println("i:"+i+" "+chessman[i]);
            //for(int i=0; i<45; ++i)
            //    System.out.println("i:"+i+" "+targetpoint[i].x+" "+targetpoint[i].y);
            if (S_DEPTH >= 2 && count.getNumb() > S_WIDTH + 2) {
                int[] value = new int[125];
                cur = 0;
                maxvalue = -10000;
                while (cur < count.getNumb()) {
                    // sai khi cur=
                    //MyWriter fw = new MyWriter("D:\\beforeSearchnb.txt");
                    //fw.WriteIntMatrix(tmap, 11, 12, "tmap");
                    //fw.WritePointMatrix(tmanposition, 32, "tmanposition");
                    //fw.WritePointMatrix(targetpoint, 125, "targetpoint");

                    curvalue = Search(tmap, tmanposition, tside, chessman[cur], targetpoint[cur], -10000, S_DEPTH - 2);
                    value[cur] = curvalue;
                    if (curvalue > maxvalue) {
                        maxvalue = curvalue;
                    }
                    cur++;
                }
                quickSort(value, chessman, targetpoint, 0, count.getNumb() - 1);
                count.setNumb(S_WIDTH);
            }
            maxvalue = -10000;
            cur = 0;
            while (cur < count.getNumb()) {
                curvalue = Search(tmap, tmanposition, tside, chessman[cur], targetpoint[cur], maxvalue, S_DEPTH);
                if (curvalue > maxvalue) {
                    maxvalue = curvalue;
                    resultman.setNumb(chessman[cur]);
                    resultpoint.x = targetpoint[cur].x;
                    resultpoint.y = targetpoint[cur].y;
                }
                cur++;
            }
            return true;
        } else {
            if (count.getNumb() > 0) {
                resultman.setNumb(chessman[count.getNumb() - 1]);
                resultpoint.x = targetpoint[count.getNumb() - 1].x;
                resultpoint.y = targetpoint[count.getNumb() - 1].y;
                return true;
            } else {
                resultman.setNumb(32);
                resultpoint.x = 0;
                return false;
            }
        }

    }

    int Search(int[][] tmap, POINT[] tmanposition, IntegerWrapper tside, int man, POINT point, int upmax, int depth) {
        int ate, cur, maxvalue, curvalue, xs, ys;
        IntegerWrapper count = new IntegerWrapper(0);

        ate = 32;
        //man moved
        xs = tmanposition[man].x;
        ys = tmanposition[man].y;			//original position

        int cp = 1;
        if (tside.getNumb() != 0) {
            cp = 0;
        }
        //if (SideOfMan[tmap[point.x][point.y]]==tside.getNumb()==0?1:0)	//man already exits in position?
        if (SideOfMan[tmap[point.x][point.y]] == cp) //man already exits in position?
        {
            ate = tmap[point.x][point.y];					//record eaten man
            if (ate == 0 || ate == 16) {
                return 9999;
            }
            tmanposition[ate].x = 0;						//eat target man
        }
        depth--;
        tmap[point.x][point.y] = man;
        tmap[xs][ys] = 32;							//move on map
        tmanposition[man].x = point.x;
        tmanposition[man].y = point.y;
        //tside=tside.getNumb()==0?1:0;
        if (tside.getNumb() != 0) {
            tside.setNumb(0);
        } else {
            tside.setNumb(1);
        }

        if (depth > 0) {
            int[] chessman = new int[125];
            POINT[] targetpoint = new POINT[125];
            for (int i = 0; i < 125; ++i) {
                targetpoint[i] = new POINT();
            }

            if (EnumList(tmap, tmanposition, tside, chessman, targetpoint, count)) {
                //MyWriter fw = new MyWriter("D:\\insideSearchnb.txt");
                //fw.WriteIntMatrix(tmap, 11, 12, "tmap");
                //fw.WritePointMatrix(tmanposition, 32, "tmanposition");
                //fw.WritePointMatrix(targetpoint, 125, "targetpoint");
                if (depth >= 2 && count.getNumb() > S_WIDTH + 2) {
                    int[] value = new int[125];
                    cur = 0;
                    maxvalue = -10000;
                    while (cur < count.getNumb()) {
                        curvalue = Search(tmap, tmanposition, tside, chessman[cur], targetpoint[cur], -10000, depth - 2);
                        value[cur] = curvalue;
                        if (curvalue > maxvalue) {
                            maxvalue = curvalue;
                        }
                        cur++;
                    }
                    quickSort(value, chessman, targetpoint, 0, count.getNumb() - 1);
                    count.setNumb(S_WIDTH);
                }

                maxvalue = -10000;
                cur = 0;
                while (cur < count.getNumb()) {
                    curvalue = Search(tmap, tmanposition, tside, chessman[cur], targetpoint[cur], maxvalue, depth);
                    if (curvalue > maxvalue) {
                        maxvalue = curvalue;
                    }
                    if (curvalue >= -upmax) {
                        break;
                    }
                    cur++;
                }
            } else {
                maxvalue = 9800;
            }
        } else {
            maxvalue = Value(tmap, tmanposition, tside);
        }

        tmanposition[man].x = xs;
        tmanposition[man].y = ys;//restore face
        tmap[xs][ys] = man;//restore map
        if (ate != 32) {
            tmanposition[ate].x = point.x;
            tmanposition[ate].y = point.y;
            tmap[point.x][point.y] = ate;
        } else {
            tmap[point.x][point.y] = 32;
        }

        if (tside.getNumb() != 0) {
            tside.setNumb(0);
        } else {
            tside.setNumb(1);
        }
        return -maxvalue;
    }

    public void ContactV(int[][] tmap, POINT[] tmanposition, IntegerWrapper tside, int[] activity, int[][] contact) {
        for (nCV = 0; nCV <= 31; nCV++) {
            xCV = tmanposition[nCV].x;
            if (xCV == 0) {
                continue;
            }
            yCV = tmanposition[nCV].y;
            switch (nCV) {
                case 0:
                    if (tmanposition[0].x == tmanposition[16].x) //+½-º++-¼-+--
                    {
                        flagCV = false;
                        for (jCV = tmanposition[16].y + 1; jCV < tmanposition[0].y; jCV++) {
                            if (tmap[xCV][jCV] != 32) {
                                flagCV = true;
                                break;
                            }
                        }
                        if (!flagCV) {
                            {
                                kCV = tmap[xCV][tmanposition[16].y];
                                activity[0] += 1;
                                if (kCV != 32) {
                                    contact[0][kCV] = 1;
                                }
                            }
                        }
                    }
                    jCV = yCV + 1;
                    if (jCV <= 10) {
                        kCV = tmap[xCV][jCV];
                        activity[0] += 1;
                        if (kCV != 32) {
                            contact[0][kCV] = 1;
                        }
                    }
                    jCV = yCV - 1;
                    if (jCV >= 8) {
                        kCV = tmap[xCV][jCV];
                        activity[0] += 1;
                        if (kCV != 32) {
                            contact[0][kCV] = 1;
                        }
                    }
                    iCV = xCV + 1;
                    if (iCV <= 6) {
                        kCV = tmap[iCV][yCV];
                        activity[0] += 1;
                        if (kCV != 32) {
                            contact[0][kCV] = 1;
                        }
                    }
                    iCV = xCV - 1;
                    if (iCV >= 4) {
                        kCV = tmap[iCV][yCV];
                        activity[0] += 1;
                        if (kCV != 32) {
                            contact[0][kCV] = 1;
                        }
                    }
                    break;
                case 16:
                    if (tmanposition[0].x == tmanposition[16].x) //+½-º++-¼-+--
                    {
                        flagCV = false;
                        for (jCV = tmanposition[16].y + 1; jCV < tmanposition[0].y; jCV++) {
                            if (tmap[xCV][jCV] != 32) {
                                flagCV = true;
                                break;
                            }
                        }
                        if (!flagCV) {
                            {
                                kCV = tmap[xCV][tmanposition[0].y];
                                activity[16] += 1;
                                if (kCV != 32) {
                                    contact[16][kCV] = 1;
                                }
                            }
                        }
                    }
                    jCV = yCV + 1;
                    if (jCV <= 3) {
                        kCV = tmap[xCV][jCV];
                        activity[16] += 1;
                        if (kCV != 32) {
                            contact[16][kCV] = 1;
                        }
                    }
                    jCV = yCV - 1;
                    if (jCV >= 1) {
                        kCV = tmap[xCV][jCV];
                        activity[16] += 1;
                        if (kCV != 32) {
                            contact[16][kCV] = 1;
                        }
                    }
                    iCV = xCV + 1;
                    if (iCV <= 6) {
                        kCV = tmap[iCV][yCV];
                        activity[16] += 1;
                        if (kCV != 32) {
                            contact[16][kCV] = 1;
                        }
                    }
                    iCV = xCV - 1;
                    if (iCV >= 4) {
                        kCV = tmap[iCV][yCV];
                        activity[16] += 1;
                        if (kCV != 32) {
                            contact[16][kCV] = 1;
                        }
                    }
                    break;
                case 1:
                case 2:
                    iCV = xCV + 1;
                    jCV = yCV + 1;
                    if (iCV <= 6 && jCV <= 10) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    iCV = xCV + 1;
                    jCV = yCV - 1;
                    if (iCV <= 6 && jCV >= 8) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    iCV = xCV - 1;
                    jCV = yCV + 1;
                    if (iCV >= 4 && jCV <= 10) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    iCV = xCV - 1;
                    jCV = yCV - 1;
                    if (iCV >= 4 && jCV >= 8) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    break;
                case 17:
                case 18:
                    iCV = xCV + 1;
                    jCV = yCV + 1;
                    if (iCV <= 6 && jCV <= 3) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    iCV = xCV + 1;
                    jCV = yCV - 1;
                    if (iCV <= 6 && jCV >= 1) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    iCV = xCV - 1;
                    jCV = yCV + 1;
                    if (iCV >= 4 && jCV <= 3) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    iCV = xCV - 1;
                    jCV = yCV - 1;
                    if (iCV >= 4 && jCV >= 1) {
                        kCV = tmap[iCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    break;
                case 3:
                case 4:
                    iCV = xCV + 2;
                    jCV = yCV + 2;
                    if (iCV <= 9 && jCV <= 10) {
                        if (NOMAN(tmap, xCV + 1, yCV + 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV + 2;
                    jCV = yCV - 2;
                    if (iCV <= 9 && jCV >= 6) {
                        if (NOMAN(tmap, xCV + 1, yCV - 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV - 2;
                    jCV = yCV + 2;
                    if (iCV >= 1 && jCV <= 10) {
                        if (NOMAN(tmap, xCV - 1, yCV + 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV - 2;
                    jCV = yCV - 2;
                    if (iCV >= 1 && jCV >= 6) {
                        if (NOMAN(tmap, xCV - 1, yCV - 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    break;
                case 19:
                case 20:
                    iCV = xCV + 2;
                    jCV = yCV + 2;
                    if (iCV <= 9 && jCV <= 5) {
                        if (NOMAN(tmap, xCV + 1, yCV + 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV + 2;
                    jCV = yCV - 2;
                    if (iCV <= 9 && jCV >= 1) {
                        if (NOMAN(tmap, xCV + 1, yCV - 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV - 2;
                    jCV = yCV + 2;
                    if (iCV >= 1 && jCV <= 5) {
                        if (NOMAN(tmap, xCV - 1, yCV + 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV - 2;
                    jCV = yCV - 2;
                    if (iCV >= 1 && jCV >= 1) {
                        if (NOMAN(tmap, xCV - 1, yCV - 1)) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    break;
                case 5:
                case 6:
                    iCV = xCV + 1;
                    if (NOMAN(tmap, iCV, yCV)) {
                        iCV = xCV + 2;
                        jCV = yCV + 1;
                        if (iCV <= 9 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV + 2;
                        jCV = yCV - 1;
                        if (iCV <= 9 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV - 1;
                    if (NOMAN(tmap, iCV, yCV)) {
                        iCV = xCV - 2;
                        jCV = yCV + 1;
                        if (iCV >= 1 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 2;
                        jCV = yCV - 1;
                        if (iCV >= 1 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    jCV = yCV + 1;
                    if (NOMAN(tmap, xCV, jCV)) {
                        iCV = xCV + 1;
                        jCV = yCV + 2;
                        if (iCV <= 9 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 1;
                        jCV = yCV + 2;
                        if (iCV >= 1 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    jCV = yCV - 1;
                    if (NOMAN(tmap, xCV, jCV)) {
                        iCV = xCV + 1;
                        jCV = yCV - 2;
                        if (iCV <= 9 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 1;
                        jCV = yCV - 2;
                        if (iCV >= 1 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    break;
                case 21:
                case 22:
                    iCV = xCV + 1;
                    if (NOMAN(tmap, iCV, yCV)) {
                        iCV = xCV + 2;
                        jCV = yCV + 1;
                        if (iCV <= 9 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV + 2;
                        jCV = yCV - 1;
                        if (iCV <= 9 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    iCV = xCV - 1;
                    if (NOMAN(tmap, iCV, yCV)) {
                        iCV = xCV - 2;
                        jCV = yCV + 1;
                        if (iCV >= 1 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 2;
                        jCV = yCV - 1;
                        if (iCV >= 1 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    jCV = yCV + 1;
                    if (NOMAN(tmap, xCV, jCV)) {
                        iCV = xCV + 1;
                        jCV = yCV + 2;
                        if (iCV <= 9 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 1;
                        jCV = yCV + 2;
                        if (iCV >= 1 && jCV <= 10) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    jCV = yCV - 1;
                    if (NOMAN(tmap, xCV, jCV)) {
                        iCV = xCV + 1;
                        jCV = yCV - 2;
                        if (iCV <= 9 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 1;
                        jCV = yCV - 2;
                        if (iCV >= 1 && jCV >= 1) {
                            kCV = tmap[iCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    break;

                case 7:
                case 8:
                    iCV = xCV + 1;
                    while (iCV <= 9) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        iCV++;
                    }
                    iCV = xCV - 1;
                    while (iCV >= 1) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        iCV--;
                    }
                    jCV = yCV + 1;
                    while (jCV <= 10) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            kCV = tmap[xCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        jCV++;
                    }
                    jCV = yCV - 1;
                    while (jCV >= 1) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            kCV = tmap[xCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        jCV--;
                    }
                    break;
                case 23:
                case 24:
                    iCV = xCV + 1;
                    while (iCV <= 9) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        iCV++;
                    }
                    iCV = xCV - 1;
                    while (iCV >= 1) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        iCV--;
                    }
                    jCV = yCV + 1;
                    while (jCV <= 10) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            kCV = tmap[xCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        jCV++;
                    }
                    jCV = yCV - 1;
                    while (jCV >= 1) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            kCV = tmap[xCV][jCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        } else {
                            {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                            break;
                        }
                        jCV--;
                    }
                    break;
                case 9:
                case 10:
                    iCV = xCV + 1;
                    flagCV = false;
                    while (iCV <= 9) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            if (!flagCV) {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[iCV][yCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        iCV++;
                    }

                    iCV = xCV - 1;
                    flagCV = false;
                    while (iCV >= 1) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            if (!flagCV) {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[iCV][yCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        iCV--;
                    }

                    jCV = yCV + 1;
                    flagCV = false;
                    while (jCV <= 10) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            if (!flagCV) {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[xCV][jCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        jCV++;
                    }

                    jCV = yCV - 1;
                    flagCV = false;
                    while (jCV >= 1) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            if (!flagCV) {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[xCV][jCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        jCV--;
                    }
                    break;

                case 25:
                case 26:
                    iCV = xCV + 1;
                    flagCV = false;
                    while (iCV <= 9) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            if (!flagCV) {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[iCV][yCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        iCV++;
                    }

                    iCV = xCV - 1;
                    flagCV = false;
                    while (iCV >= 1) {
                        if (NOMAN(tmap, iCV, yCV)) {
                            if (!flagCV) {
                                kCV = tmap[iCV][yCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[iCV][yCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        iCV--;
                    }

                    jCV = yCV + 1;
                    flagCV = false;
                    while (jCV <= 10) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            if (!flagCV) {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[xCV][jCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        jCV++;
                    }

                    jCV = yCV - 1;
                    flagCV = false;
                    while (jCV >= 1) {
                        if (NOMAN(tmap, xCV, jCV)) {
                            if (!flagCV) {
                                kCV = tmap[xCV][jCV];
                                activity[nCV] += 1;
                                if (kCV != 32) {
                                    contact[nCV][kCV] = 1;
                                }
                            }
                        } else {
                            if (!flagCV) {
                                flagCV = true;
                            } else {
                                {
                                    kCV = tmap[xCV][jCV];
                                    activity[nCV] += 1;
                                    if (kCV != 32) {
                                        contact[nCV][kCV] = 1;
                                    }
                                }
                                break;
                            }
                        }
                        jCV--;
                    }
                    break;
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                    jCV = yCV - 1;
                    if (jCV >= 1) {
                        kCV = tmap[xCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    if (yCV <= 5) {
                        iCV = xCV + 1;
                        if (iCV <= 9) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 1;
                        if (iCV >= 1) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    break;

                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                    jCV = yCV + 1;
                    if (jCV <= 10) {
                        kCV = tmap[xCV][jCV];
                        activity[nCV] += 1;
                        if (kCV != 32) {
                            contact[nCV][kCV] = 1;
                        }
                    }
                    if (yCV >= 6) {
                        iCV = xCV + 1;
                        if (iCV <= 9) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                        iCV = xCV - 1;
                        if (iCV >= 1) {
                            kCV = tmap[iCV][yCV];
                            activity[nCV] += 1;
                            if (kCV != 32) {
                                contact[nCV][kCV] = 1;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    boolean EnumList(int[][] tmap, POINT[] tmanposition, IntegerWrapper tside, int[] chessman, POINT[] move, IntegerWrapper count) {
        count.setNumb(0);
        for (nE = FistOfSide[tside.getNumb()]; nE <= LastOfSide[tside.getNumb()]; nE++) {
            xE = tmanposition[nE].x;
            if (xE == 0) {
                continue;
            }
            yE = tmanposition[nE].y;
            switch (nE) {
                case 0:
                    if (tmanposition[0].x == tmanposition[16].x) //kings are in same row
                    {
                        flagE = false;
                        for (jE = tmanposition[16].y + 1; jE < tmanposition[0].y; jE++) {
                            if (tmap[xE][jE] != 32) {
                                flagE = true;
                                break;
                            }
                        }
                        if (!flagE) {
                            {
                                chessman[count.getNumb()] = 0;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = tmanposition[16].y;
                                count.Add(1);
                                if (tmap[xE][tmanposition[16].y] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        }
                    }
                    jE = yE + 1;
                    if (jE <= 10 && NORED(tmap, xE, jE)) {
                        chessman[count.getNumb()] = 0;
                        move[count.getNumb()].x = xE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    jE = yE - 1;
                    if (jE >= 8 && NORED(tmap, xE, jE)) {
                        chessman[count.getNumb()] = 0;
                        move[count.getNumb()].x = xE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE + 1;
                    if (iE <= 6 && NORED(tmap, iE, yE)) {
                        chessman[count.getNumb()] = 0;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = yE;
                        count.Add(1);
                        if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE - 1;
                    if (iE >= 4 && NORED(tmap, iE, yE)) {
                        chessman[count.getNumb()] = 0;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = yE;
                        count.Add(1);
                        if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    break;
                case 16:
                    if (tmanposition[0].x == tmanposition[16].x) //kings in same row
                    {
                        flagE = false;
                        for (jE = tmanposition[16].y + 1; jE < tmanposition[0].y; jE++) {
                            if (tmap[xE][jE] != 32) {
                                flagE = true;
                                break;
                            }
                        }
                        if (!flagE) {
                            {
                                chessman[count.getNumb()] = 16;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = tmanposition[0].y;
                                count.Add(1);
                                if (tmap[xE][tmanposition[0].y] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        }
                    }
                    jE = yE + 1;
                    if (jE <= 3 && NOBLACK(tmap, xE, jE)) {
                        chessman[count.getNumb()] = 16;
                        move[count.getNumb()].x = xE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    jE = yE - 1;
                    if (jE >= 1 && NOBLACK(tmap, xE, jE)) {
                        chessman[count.getNumb()] = 16;
                        move[count.getNumb()].x = xE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE + 1;
                    if (iE <= 6 && NOBLACK(tmap, iE, yE)) {
                        chessman[count.getNumb()] = 16;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = yE;
                        count.Add(1);
                        if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE - 1;
                    if (iE >= 4 && NOBLACK(tmap, iE, yE)) {
                        chessman[count.getNumb()] = 16;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = yE;
                        count.Add(1);
                        if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    break;
                case 1:
                case 2:
                    iE = xE + 1;
                    jE = yE + 1;
                    if (iE <= 6 && jE <= 10 && NORED(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE + 1;
                    jE = yE - 1;
                    if (iE <= 6 && jE >= 8 && NORED(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE - 1;
                    jE = yE + 1;
                    if (iE >= 4 && jE <= 10 && NORED(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE - 1;
                    jE = yE - 1;
                    if (iE >= 4 && jE >= 8 && NORED(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    break;
                case 17:
                case 18:
                    iE = xE + 1;
                    jE = yE + 1;
                    if (iE <= 6 && jE <= 3 && NOBLACK(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE + 1;
                    jE = yE - 1;
                    if (iE <= 6 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE - 1;
                    jE = yE + 1;
                    if (iE >= 4 && jE <= 3 && NOBLACK(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    iE = xE - 1;
                    jE = yE - 1;
                    if (iE >= 4 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = iE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    break;
                case 3:
                case 4:
                    iE = xE + 2;
                    jE = yE + 2;
                    if (iE <= 9 && jE <= 10 && NORED(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE + 1, yE + 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE + 2;
                    jE = yE - 2;
                    if (iE <= 9 && jE >= 6 && NORED(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE + 1, yE - 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE - 2;
                    jE = yE + 2;
                    if (iE >= 1 && jE <= 10 && NORED(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE - 1, yE + 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE - 2;
                    jE = yE - 2;
                    if (iE >= 1 && jE >= 6 && NORED(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE - 1, yE - 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    break;
                case 19:
                case 20:
                    iE = xE + 2;
                    jE = yE + 2;
                    if (iE <= 9 && jE <= 5 && NOBLACK(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE + 1, yE + 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE + 2;
                    jE = yE - 2;
                    if (iE <= 9 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE + 1, yE - 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE - 2;
                    jE = yE + 2;
                    if (iE >= 1 && jE <= 5 && NOBLACK(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE - 1, yE + 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE - 2;
                    jE = yE - 2;
                    if (iE >= 1 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                        if (NOMAN(tmap, xE - 1, yE - 1)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    break;
                case 5:
                case 6:
                    iE = xE + 1;
                    if (NOMAN(tmap, iE, yE)) {
                        iE = xE + 2;
                        jE = yE + 1;
                        if (iE <= 9 && jE <= 10 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE + 2;
                        jE = yE - 1;
                        if (iE <= 9 && jE >= 1 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE - 1;
                    if (NOMAN(tmap, iE, yE)) {
                        iE = xE - 2;
                        jE = yE + 1;
                        if (iE >= 1 && jE <= 10 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 2;
                        jE = yE - 1;
                        if (iE >= 1 && jE >= 1 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    jE = yE + 1;
                    if (NOMAN(tmap, xE, jE)) {
                        iE = xE + 1;
                        jE = yE + 2;
                        if (iE <= 9 && jE <= 10 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 1;
                        jE = yE + 2;
                        if (iE >= 1 && jE <= 10 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    jE = yE - 1;
                    if (NOMAN(tmap, xE, jE)) {
                        iE = xE + 1;
                        jE = yE - 2;
                        if (iE <= 9 && jE >= 1 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 1;
                        jE = yE - 2;
                        if (iE >= 1 && jE >= 1 && NORED(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    break;
                case 21:
                case 22:
                    iE = xE + 1;
                    if (NOMAN(tmap, iE, yE)) {
                        iE = xE + 2;
                        jE = yE + 1;
                        if (iE <= 9 && jE <= 10 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE + 2;
                        jE = yE - 1;
                        if (iE <= 9 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    iE = xE - 1;
                    if (NOMAN(tmap, iE, yE)) {
                        iE = xE - 2;
                        jE = yE + 1;
                        if (iE >= 1 && jE <= 10 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 2;
                        jE = yE - 1;
                        if (iE >= 1 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    jE = yE + 1;
                    if (NOMAN(tmap, xE, jE)) {
                        iE = xE + 1;
                        jE = yE + 2;
                        if (iE <= 9 && jE <= 10 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 1;
                        jE = yE + 2;
                        if (iE >= 1 && jE <= 10 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    jE = yE - 1;
                    if (NOMAN(tmap, xE, jE)) {
                        iE = xE + 1;
                        jE = yE - 2;
                        if (iE <= 9 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 1;
                        jE = yE - 2;
                        if (iE >= 1 && jE >= 1 && NOBLACK(tmap, iE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[iE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    break;

                case 7:
                case 8:
                    iE = xE + 1;
                    while (iE <= 9) {
                        if (NOMAN(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NORED(tmap, iE, yE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        iE++;
                    }
                    iE = xE - 1;
                    while (iE >= 1) {
                        if (NOMAN(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NORED(tmap, iE, yE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        iE--;
                    }
                    jE = yE + 1;
                    while (jE <= 10) {
                        if (NOMAN(tmap, xE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = xE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NORED(tmap, xE, jE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        jE++;
                    }
                    jE = yE - 1;
                    while (jE >= 1) {
                        if (NOMAN(tmap, xE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = xE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NORED(tmap, xE, jE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        jE--;
                    }
                    break;
                case 23:
                case 24:
                    iE = xE + 1;
                    while (iE <= 9) {
                        if (NOMAN(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NOBLACK(tmap, iE, yE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        iE++;
                    }
                    iE = xE - 1;
                    while (iE >= 1) {
                        if (NOMAN(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NOBLACK(tmap, iE, yE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        iE--;
                    }
                    jE = yE + 1;
                    while (jE <= 10) {
                        if (NOMAN(tmap, xE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = xE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NOBLACK(tmap, xE, jE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        jE++;
                    }
                    jE = yE - 1;
                    while (jE >= 1) {
                        if (NOMAN(tmap, xE, jE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = xE;
                            move[count.getNumb()].y = jE;
                            count.Add(1);
                            if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        } else {
                            if (NOBLACK(tmap, xE, jE)) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                            break;
                        }
                        jE--;
                    }
                    break;
                case 9:
                case 10:
                    iE = xE + 1;
                    flagE = false;
                    while (iE <= 9) {
                        if (NOMAN(tmap, iE, yE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NORED(tmap, iE, yE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = iE;
                                    move[count.getNumb()].y = yE;
                                    count.Add(1);
                                    if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        iE++;
                    }

                    iE = xE - 1;
                    flagE = false;
                    while (iE >= 1) {
                        if (NOMAN(tmap, iE, yE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NORED(tmap, iE, yE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = iE;
                                    move[count.getNumb()].y = yE;
                                    count.Add(1);
                                    if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        iE--;
                    }

                    jE = yE + 1;
                    flagE = false;
                    while (jE <= 10) {
                        if (NOMAN(tmap, xE, jE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NORED(tmap, xE, jE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = xE;
                                    move[count.getNumb()].y = jE;
                                    count.Add(1);
                                    if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        jE++;
                    }

                    jE = yE - 1;
                    flagE = false;
                    while (jE >= 1) {
                        if (NOMAN(tmap, xE, jE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NORED(tmap, xE, jE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = xE;
                                    move[count.getNumb()].y = jE;
                                    count.Add(1);
                                    if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        jE--;
                    }
                    break;

                case 25:
                case 26:
                    iE = xE + 1;
                    flagE = false;
                    while (iE <= 9) {
                        if (NOMAN(tmap, iE, yE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NOBLACK(tmap, iE, yE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = iE;
                                    move[count.getNumb()].y = yE;
                                    count.Add(1);
                                    if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        iE++;
                    }

                    iE = xE - 1;
                    flagE = false;
                    while (iE >= 1) {
                        if (NOMAN(tmap, iE, yE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = iE;
                                move[count.getNumb()].y = yE;
                                count.Add(1);
                                if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NOBLACK(tmap, iE, yE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = iE;
                                    move[count.getNumb()].y = yE;
                                    count.Add(1);
                                    if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        iE--;
                    }

                    jE = yE + 1;
                    flagE = false;
                    while (jE <= 10) {
                        if (NOMAN(tmap, xE, jE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NOBLACK(tmap, xE, jE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = xE;
                                    move[count.getNumb()].y = jE;
                                    count.Add(1);
                                    if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        jE++;
                    }

                    jE = yE - 1;
                    flagE = false;
                    while (jE >= 1) {
                        if (NOMAN(tmap, xE, jE)) {
                            if (!flagE) {
                                chessman[count.getNumb()] = nE;
                                move[count.getNumb()].x = xE;
                                move[count.getNumb()].y = jE;
                                count.Add(1);
                                if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                    return false;
                                }
                            }
                        } else {
                            if (!flagE) {
                                flagE = true;
                            } else {
                                if (NOBLACK(tmap, xE, jE)) {
                                    chessman[count.getNumb()] = nE;
                                    move[count.getNumb()].x = xE;
                                    move[count.getNumb()].y = jE;
                                    count.Add(1);
                                    if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                        return false;
                                    }
                                }
                                break;
                            }
                        }
                        jE--;
                    }
                    break;
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                    jE = yE - 1;
                    if (jE >= 1 && NORED(tmap, xE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = xE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    if (yE <= 5) {
                        iE = xE + 1;
                        if (iE <= 9 && NORED(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 1;
                        if (iE >= 1 && NORED(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    break;

                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                    jE = yE + 1;
                    if (jE <= 10 && NOBLACK(tmap, xE, jE)) {
                        chessman[count.getNumb()] = nE;
                        move[count.getNumb()].x = xE;
                        move[count.getNumb()].y = jE;
                        count.Add(1);
                        if (tmap[xE][jE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                            return false;
                        }
                    }
                    if (yE >= 6) {
                        iE = xE + 1;
                        if (iE <= 9 && NOBLACK(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                        iE = xE - 1;
                        if (iE >= 1 && NOBLACK(tmap, iE, yE)) {
                            chessman[count.getNumb()] = nE;
                            move[count.getNumb()].x = iE;
                            move[count.getNumb()].y = yE;
                            count.Add(1);
                            if (tmap[iE][yE] == FistOfSide[tside.getNumb() == 0 ? 1 : 0]) {
                                return false;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        return true;

    }

    // rules chess
    public boolean CanGo(int[][] manmap, int man, POINT from, POINT to) {
        //static int i,j;
        int i, j;
        i = 0;
        j = 0; // for testing

        if (!IsNormal(ManToType[man], to)) //can it be in this position?
        {
            //check if king
            if (ManToType[man] != RED_K && ManToType[man] != BLACK_K) {
                return false;
            } else if (ManToType[man] == RED_K && //target is red king
                    ManToType[manmap[to.x][to.y]] == BLACK_K) //target is black king
            {
                boolean flag = false;
                for (j = from.y - 1; j > 0; j--) {
                    if (manmap[from.x][j] != 32) {
                        if (ManToType[manmap[from.x][j]] == BLACK_K) //++-_
                        {
                            flag = true;
                        }
                        break;
                    }
                }
                return flag;
            } else if (ManToType[manmap[to.x][to.y]] == RED_K) //+_¦--¦+½,-+__-¦-º							
            {
                boolean flag = false;
                for (j = from.y + 1; j < 11; j++) {
                    if (manmap[from.x][j] != 32) {
                        if (ManToType[manmap[from.x][j]] == RED_K) //++-_
                        {
                            flag = true;
                        }
                        break;
                    }
                }
                return flag;
            } else {
                return false;
            }
        }
        if (SideOfMan[man] == 0) {
            if (manmap[to.x][to.y] != 32 && SideOfMan[manmap[to.x][to.y]] == 0) {
                return false;
            }
        } else if (SideOfMan[man] == 1) {
            if (manmap[to.x][to.y] != 32 && SideOfMan[manmap[to.x][to.y]] == 1) {
                return false;
            }
        }
	//--------------------------------

        //rules for all man
        switch (ManToType[man]) {
            case RED_B:
                //bing can't turn backward
                if (to.y > from.y) {
                    return false;
                }
                //bing move straight line
                if (from.y - to.y + Math.abs(to.x - from.x) > 1) {
                    return false;
                }
                break;

            case BLACK_B:
                //bing can't turn backward
                if (to.y < from.y) {
                    return false;
                }
                //bing move straight line
                if (to.y - from.y + Math.abs(to.x - from.x) > 1) {
                    return false;
                }
                break;

            case RED_S:
            case BLACK_S:
                //shi move diagonally
                if (Math.abs(from.y - to.y) > 1 || Math.abs(to.x - from.x) > 1) {
                    return false;
                }
                break;

            case RED_X:
            case BLACK_X:
                //xiang move diagonally
                if (Math.abs(from.x - to.x) != 2 || Math.abs(from.y - to.y) != 2) {
                    return false;
                }
                //-_--:
                if (manmap[(from.x + to.x) / 2][(from.y + to.y) / 2] != 32) {
                    return false;
                }
                break;

            case RED_K:
            case BLACK_K:
                //+½-º+++_-+_++_-_:
                if (Math.abs(from.y - to.y) + Math.abs(to.x - from.x) > 1) {
                    return false;
                }
                break;

            case RED_J:
            case BLACK_J:
                //¦¦++-_+_+_-_:
                if (from.y != to.y && from.x != to.x) {
                    return false;
                }
                //¦¦+¡¦²¦--+-_+-_+-_+-¦_++: -----------
                if (from.y == to.y) {
                    if (from.x < to.x) {
                        for (i = from.x + 1; i < to.x; i++) {
                            if (manmap[i][from.y] != 32) {
                                return false;
                            }
                        }
                    } else {
                        for (i = to.x + 1; i < from.x; i++) {
                            if (manmap[i][from.y] != 32) {
                                return false;
                            }
                        }
                    }
                } else {
                    if (from.y < to.y) {
                        for (j = from.y + 1; j < to.y; j++) {
                            if (manmap[from.x][j] != 32) {
                                return false;
                            }
                        }
                    } else {
                        for (j = to.y + 1; j < from.y; j++) {
                            if (manmap[from.x][j] != 32) {
                                return false;
                            }
                        }
                    }
                }
                //-++--¦¦¦---------------------------------
                break;

            case RED_P:
            case BLACK_P:
                //++++-_+_+_-_:
                if (from.y != to.y && from.x != to.x) {
                    return false;
                }
                //++_+¦+++-_+¡¦²¦--+-_+-_+-_+-¦_++:------------------
                if (manmap[to.x][to.y] == 32) {
                    if (from.y == to.y) {
                        if (from.x < to.x) {
                            for (i = from.x + 1; i < to.x; i++) {
                                if (manmap[i][from.y] != 32) {
                                    return false;
                                }
                            }
                        } else {
                            for (i = to.x + 1; i < from.x; i++) {
                                if (manmap[i][from.y] != 32) {
                                    return false;
                                }
                            }
                        }
                    } else {
                        if (from.y < to.y) {
                            for (j = from.y + 1; j < to.y; j++) {
                                if (manmap[from.x][j] != 32) {
                                    return false;
                                }
                            }
                        } else {
                            for (j = to.y + 1; j < from.y; j++) {
                                if (manmap[from.x][j] != 32) {
                                    return false;
                                }
                            }
                        }
                    }
                } else {
                    int count = 0;
                    if (from.y == to.y) {
                        if (from.x < to.x) {
                            for (i = from.x + 1; i < to.x; i++) {
                                if (manmap[i][from.y] != 32) {
                                    count++;
                                }
                            }
                            if (count != 1) {
                                return false;
                            }
                        } else {
                            for (i = to.x + 1; i < from.x; i++) {
                                if (manmap[i][from.y] != 32) {
                                    count++;
                                }
                            }
                            if (count != 1) {
                                return false;
                            }
                        }
                    } else {
                        if (from.y < to.y) {
                            for (j = from.y + 1; j < to.y; j++) {
                                if (manmap[from.x][j] != 32) {
                                    count++;
                                }
                            }
                            if (count != 1) {
                                return false;
                            }
                        } else {
                            for (j = to.y + 1; j < from.y; j++) {
                                if (manmap[from.x][j] != 32) {
                                    count++;
                                }
                            }
                            if (count != 1) {
                                return false;
                            }
                        }
                    }
                }
                break;

            case RED_M:
            case BLACK_M:
                if (!((Math.abs(to.x - from.x) == 1 && Math.abs(to.y - from.y) == 2)
                        || (Math.abs(to.x - from.x) == 2 && Math.abs(to.y - from.y) == 1))) {
                    return false;
                }

                if (to.x - from.x == 2) {
                    i = from.x + 1;
                    j = from.y;
                } else if (from.x - to.x == 2) {
                    i = from.x - 1;
                    j = from.y;
                } else if (to.y - from.y == 2) {
                    i = from.x;
                    j = from.y + 1;
                } else if (from.y - to.y == 2) {
                    i = from.x;
                    j = from.y - 1;
                }
                if (manmap[i][j] != 32) {
                    return false;
                }
                break;

            default:
                break;
        }

        return true;

    }

    public boolean IsNormal(int mantype, POINT point) {
        if (point.x < 1 || point.x > 9 || point.y < 1 || point.y > 10) {
            return false;
        }
        switch (mantype) {
            case RED_K:
                if (point.x > 6 || point.x < 4 || point.y < 8) {
                    return false;
                }
                break;
            case RED_S:
                if (!((point.x == 4 && point.y == 10)
                        || (point.x == 4 && point.y == 8)
                        || (point.x == 5 && point.y == 9)
                        || (point.x == 6 && point.y == 10)
                        || (point.x == 6 && point.y == 8))) {
                    return false;
                }
                break;

            case RED_X:
                //¦_+÷-_++:
                if (!((point.x == 1 && point.y == 8)
                        || (point.x == 3 && point.y == 10)
                        || (point.x == 3 && point.y == 6)
                        || (point.x == 5 && point.y == 8)
                        || (point.x == 7 && point.y == 10)
                        || (point.x == 7 && point.y == 6)
                        || (point.x == 9 && point.y == 8))) {
                    return false;
                }
                break;

            case RED_B:
                //_°_+-_++_°++¦_:
                if (point.y > 7) {
                    return false;
                }
                //_°¦²¦+¦__+-_+_+--¦¦»:
                if (point.y > 5 && point.x % 2 == 0) {
                    return false;
                }
                break;

            case BLACK_K:
                //-º_+-_++¦_++¦¼-_:
                if (point.x > 6 || point.x < 4 || point.y > 3) {
                    return false;
                }
                break;

            case BLACK_S:
                //--++-_++¦¼-+¦+¦¿¦¶:
                if (!((point.x == 4 && point.y == 1)
                        || (point.x == 4 && point.y == 3)
                        || (point.x == 5 && point.y == 2)
                        || (point.x == 6 && point.y == 1)
                        || (point.x == 6 && point.y == 3))) {
                    return false;
                }
                break;

            case BLACK_X:
                //¦_+÷-_++:
                if (!((point.x == 1 && point.y == 3)
                        || (point.x == 3 && point.y == 1)
                        || (point.x == 3 && point.y == 5)
                        || (point.x == 5 && point.y == 3)
                        || (point.x == 7 && point.y == 1)
                        || (point.x == 7 && point.y == 5)
                        || (point.x == 9 && point.y == 3))) {
                    return false;
                }
                break;

            case BLACK_B:
                //_°_+-_++_°++¦_:
                if (point.y < 4) {
                    return false;
                }
                //_°¦²¦+¦__+-_+_+--¦¦»:
                if (point.y < 6 && point.x % 2 == 0) {
                    return false;
                }
                break;

            default:
                break;
        }
        return true;

    }
}
